import types from '../mutation-types'
import services from '../../services'

const state = {
  message: '',
  sliders: [],
  latestUpdate: {},
  recommended: {},
  selected: {}
}

const getters = {
  latestUpdateCount: state => state.latestUpdate.length,
  recommendedCount: state => state.recommended.length
}

const mutations = {
  [types.SELECT_BOOK]: (state, params) => {
    state.selected = params
  },
  [types.SET_BOOKS]: (state, params) => {
    for (let prop in state) {
      state[prop] = params[prop]
    }
    state.selected = {anthor: []}
  }
}

const actions = {
  getItems (ctx) {
    services.products.get().then(res => {
      console.log(res.body)
      ctx.commit(types.SET_BOOKS, res.body)
    }).catch(error => {
      console.log(`error: ${error}`)
    })
  }
}
export default {
  state,
  getters,
  mutations,
  actions
}
